package com.toolenaar.socialiceThis;

import java.io.IOException;
import java.net.MalformedURLException;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

public class Main extends TabActivity {
    /** Called when the activity is first created. */
	public static TabHost mapTabhost;
	private InviteFriends _invite;
	FrameLayout mapfrLayout;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        mapTabhost = getTabHost();
        TabSpec tabSpec = mapTabhost.newTabSpec("tab_1");
        tabSpec.setIndicator("Map");
        Context ctx = this.getApplicationContext();
        Intent intent = new Intent(ctx,MapTabView.class);
        tabSpec.setContent(intent); 
        TabSpec friendsSpec = mapTabhost.newTabSpec("tab_2");
        friendsSpec.setIndicator("friends");
        Intent friendsIntent = new Intent(ctx, FriendsTabView.class);
        friendsSpec.setContent(friendsIntent);
        mapTabhost.addTab(tabSpec);
        mapTabhost.addTab(friendsSpec);
        mapTabhost.addTab(mapTabhost.newTabSpec("tab_3").setIndicator("Events").setContent(R.id.textview1));
        mapTabhost.setCurrentTab(1);
        
        startLocationUpdateService();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.invite:
        	Intent intent = new Intent(getBaseContext(), InviteFriends.class);
        	startActivityForResult(intent, 0);
            return true;
        case R.id.logout:
			if(FriendsTabView.facebook != null)
			{
				try {
					FriendsTabView.facebook.logout(getApplicationContext());
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finish();
			}
            return true;
        case R.id.settings:
			Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_SHORT).show();  
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

	private void startLocationUpdateService() {
		// TODO Auto-generated method stub
		Intent service = new Intent("com.toolenaar.socialiceThis.LocationUpdaterService.SERVICE");
		
		startService(service);
		
	}
}